package com.bops.biz;

import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

public class ControllerTools {

    /**
     * 创建Bean
     * 
     * @param value
     * @param beanName
     * @param cls
     * @return
     * @throws Exception
     */
    public static Object createBean(String value[], String beanName[], Class<?> cls)
            throws Exception {
        return  createBean(value,beanName,cls,0,0f,0d);
    }
    /**
     * 创建Bean
     * 
     * @param value
     * @param beanName
     * @param cls
     * @return
     * @throws Exception
     */
    public static Object createBean(String value[], String beanName[], Class<?> cls,Integer intDefault,Float floatDefault,Double doubleDefault)
            throws Exception {
        Object result = cls.newInstance();
        for (int i = 0; i < beanName.length; i++) {
            String methodName = beanName[i];
            methodName = methodName.substring(0, 1).toUpperCase()
                    + methodName.substring(1);
            try {
                Method getMeth = cls.getMethod("get" + methodName, null);
                Class<?> returnType = getMeth.getReturnType();
                Method meth = cls.getMethod("set" + methodName, returnType);
                if (returnType == String.class) {
                    meth.invoke(result, value[i]);
                } else if (returnType == Integer.class) {
                    meth.invoke(result, (value[i]==null || value[i].length() == 0) ? intDefault
                            : new Integer(value[i]));
                } else if (returnType == Float.class) {
                    meth.invoke(result, (value[i]==null || value[i].length() == 0) ? floatDefault
                            : new Float(value[i]));
                } else if (returnType == Double.class) {
                    meth.invoke(result, (value[i].length() == 0) ? doubleDefault
                            : new Double(value[i]));
                } else if (returnType == Date.class) {
                    meth.invoke(result, strToDate(value[i]));
                } else {
                    throw new Error("not found the method");
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return result;

    }

    /**
     * 根据request中取相应params的数组，并且检查把每一行中是否全部没有内容，如果没有就删除该行
     * 
     * @param params
     * @return
     */
    public static List<String[]> getParamValues(String params[],HttpServletRequest request) {
        List<String[]> list = new ArrayList<String[]>();
        int column = params.length;
        String data[][] = null;
        int row = 0;
        for (int i = 0; i < column; i++) {
            String param = params[i];
            String[] result = request.getParameterValues(param);
            if (result == null) {
                continue;
            }
            if (i == 0) {
                row = result.length;
                data = new String[column][row];
            }
            for (int j = 0; j < result.length; j++) {
                data[i][j] = result[j];
            }
        }
        for (int i = 0; i < row; i++) {
            String rowData[] = new String[column];
            boolean isBlank = true;
            for (int j = 0; j < column; j++) {
                rowData[j] = data[j][i];
                if (isBlank) {
                    if (rowData[j] != null && rowData[j].length() > 0) {
                        isBlank = false;
                    }
                }
            }
            if (!isBlank) {
                list.add(rowData);
            }
        }
        return list;
    }

    /**
     * 把字符串转换为日期 如果不能转换就为null
     * 
     * @param date
     * @return
     */
    public static Date strToDate(String date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return df.parse(date);
        } catch (ParseException e) {
        }
        return null;
    }
}
